package com.caimiao.miao.http.manager;

import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.http.HttpUtil;
import com.caimiao.miao.http.retorfit.HttpConfig;
import com.caimiao.miao.library.database.DBHelperFactory;
import com.caimiao.miao.library.database.table.TicketOutDBHelper;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class TicketOutHttp {

    public Disposable outTicket() {
        return Observable
                .create((ObservableOnSubscribe<List<TicketOut>>) emitter -> emitter.onNext(DBHelperFactory.getDBHelper(TicketOutDBHelper.class).getNotSyncData()))
                .flatMap((Function<List<TicketOut>, Observable<TicketOut>>) ticketOuts -> Observable.fromIterable(ticketOuts))
                .flatMap((Function<TicketOut, ObservableSource<TicketOut>>) ticket -> HttpUtil.postOutTicket(ticket)
                        .map(response -> {
                            ticket.setSync(response.getState() == HttpConfig.STATE_SUCCESS ? 1 : 2);
                            return ticket;
                        }))
                .subscribeOn(Schedulers.io())
                .subscribe(ticket -> DBHelperFactory
                                .getDBHelper(TicketOutDBHelper.class)
                                .updateSync(ticket),
                        throwable -> throwable.printStackTrace());


//        return Observable
//                .create(new ObservableOnSubscribe<TicketOut>() {
//                    @Override
//                    public void subscribe(ObservableEmitter<TicketOut> emitter) throws Exception {
//                        mEmitter = emitter;
//                        sendNext(context);
//                    }
//                })
//                .flatMap(new Function<TicketOut, ObservableSource<TicketOut>>() {
//                    @Override
//                    public ObservableSource<TicketOut> apply(TicketOut ticket) throws Exception {
//                        return HttpUtil.postOutTicket(ticket)
//                                .map(new Function<Response, TicketOut>() {
//                                    @Override
//                                    public TicketOut apply(Response response) throws Exception {
//                                        ticket.setSync(response.getState() == HttpConfig.STATE_SUCCESS ? 1 : 2);
//                                        return ticket;
//                                    }
//                                });
//                    }
//                })
//                .subscribeOn(Schedulers.io())
//                .observeOn(Schedulers.io())
//                .doOnNext(new Consumer<TicketOut>() {
//                    @Override
//                    public void accept(TicketOut ticket) throws Exception {
//                        updateUpload(context, ticket);
//                    }
//                })
//                .subscribe(new Consumer<TicketOut>() {
//                    @Override
//                    public void accept(TicketOut ticket) throws Exception {
//                        sendNext(context);
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        throwable.printStackTrace();
//                        updateToInit(context);
//                    }
//                }, new Action() {
//                    @Override
//                    public void run() throws Exception {
//                        updateToInit(context);
//                    }
//                });
    }
//
//    private ObservableEmitter<TicketOut> mEmitter;
//
//    private void sendNext(Context context) {
//        if (mEmitter != null) {
//            TicketOut ticket = DBHelperFactory.getDBHelper(TicketOutDBHelper.class).next();
//            if (ticket == null)
//                mEmitter.onComplete();
//            else
//                mEmitter.onNext(ticket);
//        }
//    }
//
//    /*更状态sync*/
//    private void updateUpload(Context context, TicketOut ticket) {
//        IDBHelper dbHelper = DBHelperFactory.getTicketOutDBHelper(context);
//        ContentValues values = dbHelper.create(ticket);
//        dbHelper.update(values, TicketOutTable.ATTR_TRADE + " = ?", new String[]{ticket.getOut_trade_no()});
//    }
//
//    /*错误sync=2改为初值=0*/
//    private void updateToInit(Context context) {
//        IDBHelper dbHelper = DBHelperFactory.getTicketOutDBHelper(context);
//        ContentValues values = new ContentValues();
//        values.put(TicketOutTable.ATTR_SYNC, 0);
//        dbHelper.update(values, TicketOutTable.ATTR_SYNC + " = ?", new String[]{"2"});
//    }
}
